package nuc.wy;

import java.util.ArrayList;
import java.util.List;

import nuc.SQLoperation.Constant;
import nuc.SQLoperation.SQLiteOperationHelper;
import nuc.widget.Parse;
import nuc.widget.WidgetActivity;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.text.method.ScrollingMovementMethod;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class NoteContent extends Activity{
	
	private TextView titleTextView = null;
	private TextView contentTextView = null;
	private TextView timeTextView = null;
	private ImageButton addNoteImageButton = null;
	private ImageButton showAllImageButton = null;
	private ImageButton deleteImageButton = null;
	private ImageButton updateImageButton = null;
	private ImageButton shareImageButton = null;
	
	private String time = null;
	
	private SimpleNote note = new SimpleNote();
	private static List<SimpleNote> notes;
	private SQLiteOperationHelper DBHelper;
	private static int position = 0;
	
	private static final int LARGE_MOVE_X = 150;
	private static final int LARGE_MOVE_Y = 100;
	private GestureDetector gestureDetector;
//	private String title = null;
//	private String content = null;
//按键修改图标类，待抽象	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.notecontent);
		
		Intent intentContent = getIntent();
		note = intentContent.getParcelableExtra("note");
		System.out.println(note);
		position = intentContent.getIntExtra("position", 0);
		System.out.println("NoteContent" + position);
		
		time = DateFormat.format("yyyy-MM-dd kk:mm", note.lastdata).toString();
		
		titleTextView = (TextView) findViewById(R.id.NoteContentTitleTextViewId);
		contentTextView = (TextView) findViewById(R.id.NoteContentTextViewId);
		//contentTextView.setMovementMethod(ScrollingMovementMethod.getInstance());
		timeTextView = (TextView) findViewById(R.id.NoteContentTimeTextViewId);
		titleTextView.setText(note.title);
		contentTextView.setText(note.content);
		timeTextView.setText(time);
		
		contentTextView.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				return false;
			}
		});
		//contentTextView.setLongClickable(true);
		
		addNoteImageButton = (ImageButton) findViewById(R.id.NoteContentAddNoteImageButtonId);
		addNoteImageButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent addIntent = new Intent(NoteContent.this,CreateNote.class);
				startActivity(addIntent);
			}
		});
		addNoteImageButton.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				if(event.getAction() == MotionEvent.ACTION_DOWN) {
					addNoteImageButton.setBackgroundResource(R.drawable.addset);
				}
				else {
					addNoteImageButton.setBackgroundResource(R.drawable.add);
				}
				return false;
			}
		});
		
		showAllImageButton = (ImageButton) findViewById(R.id.NoteContentShowAllImageButtonId);
		showAllImageButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent showAllIntent = new Intent(NoteContent.this,SimpleNoteActivity.class);
				showAllIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(showAllIntent);
				overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
				finish();
			}
		});
		showAllImageButton.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				if(event.getAction() == MotionEvent.ACTION_DOWN) {
					showAllImageButton.setBackgroundResource(R.drawable.showallset);
				}
				else {
					showAllImageButton.setBackgroundResource(R.drawable.showall);
				}
				return false;
			}
		});
		
		deleteImageButton = (ImageButton) findViewById(R.id.NoteContentDeleteImageButtonId);
		deleteImageButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				AlertDialog.Builder builder = new Builder(NoteContent.this);
				builder.setTitle(R.string.alert);
				builder.setMessage(R.string.delete_warn);
				builder.setPositiveButton(R.string.confirm_delete, new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						SQLiteOperationHelper del = new SQLiteOperationHelper(NoteContent.this, Constant.DB_NAME, null, 1);
						del.del(Long.parseLong(note.id));
						del.close();
						
						WidgetActivity widget  = new WidgetActivity();
						widget.update(getApplicationContext());
						
						Toast.makeText(NoteContent.this, R.string.delete_succeed, Toast.LENGTH_SHORT).show();
						finish();
						overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
					}
				});
				builder.setNegativeButton(R.string.cancel_delete, new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						
					}
				});
				builder.show();
			}
			
		});
		deleteImageButton.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				if(event.getAction() == MotionEvent.ACTION_DOWN) {
					deleteImageButton.setBackgroundResource(R.drawable.delset);
				}
				else {
					deleteImageButton.setBackgroundResource(R.drawable.del);
				}
				return false;
			}
		});
		
		
		updateImageButton = (ImageButton) findViewById(R.id.NoteContentUpdateImageButtonId);
		updateImageButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(NoteContent.this,CreateNote.class);
				intent.putExtra("id", Long.parseLong(note.id));
				startActivity(intent);
				finish();
				overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
			}
		});
		updateImageButton.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				if(event.getAction() == MotionEvent.ACTION_DOWN) {
					updateImageButton.setBackgroundResource(R.drawable.editset);
				}
				else {
					updateImageButton.setBackgroundResource(R.drawable.edit);
				}
				return false;
			}
		});
		
		
		shareImageButton = (ImageButton) findViewById(R.id.NoteContentShareImageButtonId);
		shareImageButton.setBackgroundResource(android.R.drawable.ic_menu_share);
		shareImageButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(Intent.ACTION_SEND);
				intent.setType("text/plain");
				intent.putExtra(Intent.EXTRA_SUBJECT, "#SimpleNote#" + note.title);
				intent.putExtra(Intent.EXTRA_TEXT, note.content);
				startActivity(Intent.createChooser(intent, getTitle()));
			}
		});
		
		gestureDetector = new GestureDetector(this,new SimpleOnGestureListener(){

			@Override
			public boolean onFling(MotionEvent e1, MotionEvent e2,
					float velocityX, float velocityY) {
				// TODO Auto-generated method stub
				NoteContent content = new NoteContent();
				if(Math.abs(e1.getX() - e2.getX())> LARGE_MOVE_X){
					finish();
					overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
				}
				else if(e1.getY() - e2.getY() > LARGE_MOVE_Y){
					position--;
					content.fetchMessages(getApplicationContext());
					content.checkPosition();
					note = notes.get(position);
					titleTextView.setText(note.title);
					contentTextView.setText(note.content);
					timeTextView.setText(DateFormat.format("yyyy-MM-dd kk:mm", note.lastdata).toString());
				}
				else if(e2.getY() - e1.getY() > LARGE_MOVE_Y){
					position++;
					content.fetchMessages(NoteContent.this);
					content.checkPosition();
					note = notes.get(position);
					titleTextView.setText(note.title);
					contentTextView.setText(note.content);
					timeTextView.setText(DateFormat.format("yyyy-MM-dd kk:mm", note.lastdata).toString());
				}
				return false;
			}
			
		});
	}
	
    @Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		return gestureDetector.onTouchEvent(event);
    }
    

	public Cursor getDb(Context context){
		DBHelper = new SQLiteOperationHelper(context, Constant.DB_NAME, null, 1);
		Cursor cursor = DBHelper.query("_lastdate desc");
		return cursor;
	}
	
	public void fetchMessages(Context context){
		if(notes == null){
			notes = new ArrayList<SimpleNote>();
		}
		else{
			notes.clear();
		}
		Cursor c = getDb(context);
		if(c != null){
			if(c.moveToFirst()){
				do{
					SimpleNote note = Parse.parseCursor(c);
					notes.add(note);
				}while(c.moveToNext());
			}
		}
		c.close();
		DBHelper.close();
	}
	
	public void checkPosition(){
		if(position < 0){
			position = 0;
		}
		else if(position >= notes.size()){
			position = notes.size()-1;
		}
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		finish();
	}
	
}
